﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;
using System.Data;

namespace smartphone.UserControls
{
    public partial class CartSummary : System.Web.UI.UserControl
    {
        private ShoppingCart cart = new ShoppingCart();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //khong cho hien gio hang o trang chi tiet gio hang, con tat ca cac trang deu hien
        protected void Page_Init(object sender, EventArgs e)
        {
            //lay dia chi trang hien tai
            string page = Request.AppRelativeCurrentExecutionFilePath;
            if (String.Compare(page, "~/ShoppingCartPage.aspx", true) == 0)
            {
                this.Visible = false;
            }
            else
            {
                this.Visible = true;
            }
        }

        //Xu kien preRender xay ra cham hon
        protected void Page_PreRender(object sender, EventArgs e)
        {
            PopulateControls();
        }

        private void PopulateControls()
        {
            DataTable table = cart.GetItems();
            if (table.Rows.Count == 0)
            {
                cartSummaryLabel.Text = "Không có sản phẩm";
                totalAmountLabel.Text = String.Format("{0:0,000}", 0) + " vnđ";
                viewCartLink.Visible = false;
                DlCartSummary.Visible = false;
            }
            else
            {
                cartSummaryLabel.Text = "Giỏ hàng";
                viewCartLink.Visible = true;
                DlCartSummary.Visible = true;
                DlCartSummary.DataSource = table;
                DlCartSummary.DataBind();
                //lay tong gia
                decimal total = cart.GetTotalAmount();
                totalAmountLabel.Text = String.Format("{0:0,000}", total) + " vnđ";
            }
        }
    }
}